<?php

require_once(dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/appconf.php');

class mapi_itrial_productApplication extends Site {

    function setup() {
        $this->run_modes('list');
        $this->start_mode('list');
        parent::setup();
    }

    function cgiapp_prerun() {

        $this->mapi_object = getModel('MAPI_Base');
        $this->Product_application = getModel('Model_ProductApplication');
        parent::cgiapp_prerun();
    }

    function action_list() {

        $page = intval($this->get_q('page', 1, 'int'));
        $pid = intval($this->get_q('id', 0, 'int'));
        $pagesize = intval($this->get_q('pagesize', 10, 'int'));
        ;
        $a_result = array();

        if ($pid == 0) {
            return $this->mapi_object->to_json(array(), array(), 0, "產品id為空");
        }
        $a_application_field = array('id', 'oid', 'application_status', 'body', 'addtime', 'status');
        //$a_member_field = array('mid', 'photo', 'username', 'email', 'age', 'body_skin');

        $kvcobj = getCache('mapi_productApplication_list', array('expire' => KVC_EXPIRE));
        //$kvcobj = getCache('mapi_productApplication_list', array('expire' => 1));
        $productApplication_list = $kvcobj->get('mapi_productApplication_list' . $pid . "_" . $page . "_" . $pagesize);
        if (!$productApplication_list) {

            $productApplication_list = $this->Product_application->getApplicationList($pid, 4, "id desc", $page, $pagesize);
            if ($productApplication_list['result']) {
                foreach ($productApplication_list['result'] as $key => $value) {
                    $h_row_app = array();
                    $h_row_member = array();

                    foreach ($a_application_field as $field) {
                        $h_row_app[$field] = $value[$field];
                    }
                    $h_row_member['id'] = $value['mid'];
                    $h_row_member['username'] = $value['username'];
                    $h_row_member['photo_url'] = imore_avatar($value['photo']);
                    $h_row_member['agegroup'] = $value['age'];
                    $h_row_member['face_skin'] = $value['body_skin'];

                    $a_result[$key]['application'] = $h_row_app;
                    $a_result[$key]['member'] = $h_row_member;
                }
                $productApplication_list['result'] = $a_result;
            }


            $kvcobj->set('mapi_productApplication_list' . $pid . "_" . $page . "_" . $pagesize, $productApplication_list);
        }

        return $this->mapi_object->to_json($productApplication_list['result'], $productApplication_list['pageinfo']);
    }

}

header("Content-type: text/html; charset=utf-8");
$webapp = new mapi_itrial_productApplication();
$webapp->run();
?>